//
//  LoginVC.m
//  MyBook
//
//  Created by zhiyou on 16-3-22.
//  Copyright (c) 2016年 zhiyou. All rights reserved.
//

#import "LoginVC.h"
#import "RegisterVC.h"
#import "Masonry.h"
#import "MainVC.h"
#import "MyTable.h"
#import "Users.h"
#import "Custom.h"
#define UserDefault [NSUserDefaults standardUserDefaults]
@interface LoginVC ()

@end

@implementation LoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     self.title=@"登录";
    _NameTF.text=[UserDefault objectForKey:@"name"];
    _PwdTF.text=[UserDefault objectForKey:@"pwd"];
    _Imageview=[[UIImageView alloc]init];
    _Imageview.image=[UIImage imageNamed:@"背景"];
    [self.view addSubview:_Imageview];
    [_Imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.3);
    }];
     _NameTF=[[UITextField alloc]init];
    _NameTF.placeholder=@"请输入用户名";
    _NameTF.textAlignment=NSTextAlignmentCenter ;
    _NameTF.borderStyle=UITextBorderStyleRoundedRect;
    UIImageView*imgV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"手机"]];
    [self.view addSubview:_NameTF];
    _NameTF.leftView=imgV;
    _NameTF.leftViewMode= UITextFieldViewModeAlways;
    [_NameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-30);
    }];
    _Namelabel=[[UILabel alloc]init];
    _Namelabel.text=@"用户名：";
    [self.view addSubview:_Namelabel];
    [_Namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_Imageview.mas_bottom).offset(30);
        make.centerY.equalTo(_NameTF.mas_centerY);
       make.right.equalTo(_NameTF.mas_left).offset(5);
        make.left.offset(15);
        
    }];

       _PwdTF=[[UITextField alloc]init];
    _PwdTF.placeholder=@"请输入密码";
    _PwdTF.textAlignment=NSTextAlignmentCenter ;
    _PwdTF.borderStyle=UITextBorderStyleRoundedRect;
    [self.view addSubview:_PwdTF];
    UIImageView*imgV1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"密码"]];
    _PwdTF.leftView=imgV1;
    _PwdTF.leftViewMode= UITextFieldViewModeAlways;
    [_PwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
               make.right.offset(-30);
    }];
    _pwdlabel=[[UILabel alloc]init];
    _pwdlabel.text=@"密码：";
    [self.view addSubview:_pwdlabel];
    [_pwdlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_Namelabel.mas_bottom).offset(20);
        make.right.equalTo(_PwdTF.mas_left).offset(5);
        make.centerY.equalTo(_PwdTF.mas_centerY);

        make.left.offset(28);
        
    }];

    _VisitorButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_VisitorButton setTitle:@"游客访问" forState:UIControlStateNormal];
    [self.view addSubview:_VisitorButton];
    _VisitorButton.tag=1;
    [_VisitorButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
    _VisitorButton.titleLabel.font=[UIFont systemFontOfSize:12];
    [_VisitorButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_VisitorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_PwdTF.mas_bottom).offset(10);
        make.right.equalTo(_PwdTF.mas_right);
              make.width.offset(70);
    }];
      _loginButton=[UIButton buttonWithType:UIButtonTypeCustom];
     [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [self.view addSubview:_loginButton];
    _loginButton.tag=2;
    [_loginButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];

  [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
      make.bottom.multipliedBy(0.8);
      make.centerX.equalTo(self.view.mas_centerX);
  }];
    _RegisterButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_RegisterButton setTitle:@"还没有账号？请注册" forState:UIControlStateNormal];
    _RegisterButton.titleLabel.font=[UIFont systemFontOfSize:12];
    [_RegisterButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    _RegisterButton.tag=3;
    [_RegisterButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:_RegisterButton];
    [_RegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
         }];
}
-(void)click:(UIButton*)button{
    if(button.tag==1){
         [Tools goToMainVC];
    }else if (button.tag==2){
        if(_NameTF.text.length!=0&&_PwdTF.text.length!=0){
            [Custom registerOrLoginRequestWithCommand:@"1" Name:_NameTF.text Psw:_PwdTF.text Nickname:nil Email:nil Comletion:^(NSString *str) {
                [Custom setMessage:str];
                NSLog(@"%@",str);
                if([str isEqualToString:@"登录成功"]){
                  [Tools goToMainVC];
                }else{
                    return ;
                }
            }];
        }
    }
      else{
        RegisterVC*vc=[[RegisterVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
